﻿@{
    ViewBag.Title = "订单详情";
    Layout = "~/Views/Shared/_layoutPersonal.cshtml";
}
@Styles.Render("~/Content/css/orderDetails")
<article class="main">
    <nav class="mod_nav">
        <div class="lay_left">
            <a class="nav_back" onclick="turnBack()"><img src="/images/TurnBackNormal.png"></a>
            <span>订单详情</span>
        </div>
    </nav>
    <section class="mod_order">
        <div class="orderInfo">
            <ul>
                <li><span id="time"></span><span id="orderid"></span></li>
                <li><span>订单状态</span><span id="OrderStatus"></span></li>
                <li class="userInfo">
                    <span class="address1"></span>
                    <span class="address2"></span>
                </li>
                <li><span id="timeLabel"></span><span id="getGoodsTime" style="color:gray;"></span></li>
                <li><span>配送费</span><span id="delivery_fee" style="color:darkgray"></span></li>
            </ul>
        </div>
        <div class="orderList">
            <ul id="orderListInfo"></ul>
        </div>
    </section>
    <section class="mod_control">
        <span class="cost"></span>

        <div class="btn_list">
            <a class="btn-quite" style="display: none;" onclick="cancelOrder()">取消订单</a>
            <a class="btn-pay" style="display: none;" onclick="payOrder()">去支付</a>
            <div class="btn-code" style="display: none;">
                <span>取货码</span>
                <span id="goods_code"></span>
            </div>
            <a class="btn-sure" style="display: none;" onclick="takeOrder()">确认收货</a>
            <a class="btn-call" style="display: none;">拨打电话</a>

        </div>
    </section>
</article>
<script>
    function turnBack() {
        OrderMgr.resetOrderChangeNotifyCallback();
        window.location.href = '@Url.Content("~/")' + 'Order/Index';
    }

    window.onload=function () {
        OrderMgr.setOrderChangeNotifyCallback(orderTakeOffCallback);
        showOrderDetails();
    }

    function showOrderDetails() {
        var curOrderInfo = OrderMgr.getCurOrderInfo();
        if (null != curOrderInfo) {
            displayDetails(curOrderInfo);
        }
    }

    function getOrderStatus(orderType, payType) {
        if (orderType == OrderMgr.failPayOrderType) {
            $("#OrderStatus").html("<span style=\"color:red;\">待支付</span>");
            $(".btn-quite").attr("style", "display:block");
            $(".btn-pay").attr("style", "display:block");
        } else if (orderType == OrderMgr.unpickedOrderType) {
            if (payType == OrderMgr.codPaytType) {
                $("#OrderStatus").text("货到付款待支付");
                $(".btn-sure").attr("style", "display:block");
                $(".btn-code").attr("style", "display:block");
            } else {
                $("#OrderStatus").text("已支付");
                $(".btn-sure").attr("style", "display:block");
                $(".btn-code").attr("style", "display:block");
            }
        } else {
            $("#OrderStatus").text("已收货");
            $(".btn-call").attr("style", "display:block");
        }
    }

    function getOrderDeliveryStatus(orderInfo, totalFee) {
        if (OrderMgr.pickedOrderType == orderInfo.order_type) {
            $("#timeLabel").text("签收时间");
            $("#getGoodsTime").text(orderInfo.picked_time);
            $(".cost").html("<span style=\"color:gray;\">共&yen;" + totalFee.toFixed(1) + "</span>");
        } else {
            $("#timeLabel").text("预计收货时间");
            var start = new Date(Number(orderInfo.buyer_info.delivery_time_start));
            var end = new Date(Number(orderInfo.buyer_info.delivery_time_end));
            $("#getGoodsTime").text(getDeliveryTime(start, end));
            $(".cost").html("共&yen;" + totalFee.toFixed(1));
        }
    }

    function displayDetails(orderInfo) {
        $("#time").text(orderInfo.time);
        $("#orderid").text("订单号： " + orderInfo.orderid);
        $(".address1").text(orderInfo.buyer_info.delivery_addr.username + " " + orderInfo.buyer_info.delivery_addr.userphone);
        $(".address2").text(orderInfo.buyer_info.delivery_addr.deliveryaddr);

        $("#delivery_fee").html("&yen;" + orderInfo.delivery_fee.toFixed(1));

        var totalFee = 0;
        $("ul#orderListInfo li").each(function () {
            $(this).remove();
        });
        for (var i = 0; i < orderInfo.store_order_goods_info.length; i++) {
            totalFee += orderInfo.store_order_goods_info[i].goods_info.price * orderInfo.store_order_goods_info[i].goods_num;
            var $li = $('<li><div class=\"good_info\"><img class=\"good_img\" src=\"' + orderInfo.store_order_goods_info[i].goods_info.image + '\"><div class=\"good_text\"><span class=\"good_name\">' + orderInfo.store_order_goods_info[i].goods_info.name + '</span><span class=\"good_cost\">&yen;' + orderInfo.store_order_goods_info[i].goods_info.price + '</span></div></div><div class=\"goog_count\">x' + orderInfo.store_order_goods_info[i].goods_num + '</div></li>');
            $("#orderListInfo").append($li);
        }
        totalFee += orderInfo.delivery_fee;
        getOrderDeliveryStatus(orderInfo, totalFee);

        getOrderStatus(orderInfo.order_type, orderInfo.pay_type);

        $("#goods_code").text(orderInfo.goods_code);
    }

    function cancelOrderCallback() {
        turnBack();
    }

    function cancelOrder() {
        var curOrderInfo = OrderMgr.getCurOrderInfo();
        OrderMgr.cancelOrder(curOrderInfo, cancelOrderCallback);
    }

    function payOrder() {
        //TODO: 这里要显示出支付方式的选择
    }

    function takeOrder() {
        var curOrderInfo = OrderMgr.getCurOrderInfo();
        OrderMgr.takeOrder(curOrderInfo);
    }

    function orderTakeOffCallback(orderInfo) {
        var curOrderInfo = OrderMgr.getCurOrderInfo();
        if ((null != curOrderInfo) && (curOrderInfo.orderid == orderInfo.orderid)) {
            $("#orderStatus").text("已收货");
            $("#timeLabel").text("签收时间");
            $("#getGoodsTime").text(orderInfo.picked_time);
            $("#tickGoodsCode").hide();
            $(".cost").css("color", "gray");
        }
    }
</script>
